package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCustomInfoBinding;
import com.example.yichuangyun.databinding.ItemImage2Binding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.company.CustomInfoDataModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.DensityUtil;
import com.example.yichuangyun.utils.pictrue.GlideEngine;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.luck.picture.lib.style.SelectMainStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * 自主信息
 */
@SuppressLint("NotifyDataSetChanged")
public class CustomInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<CustomInfoDataModel.CustomInfoModel> data;

    public CustomInfoAdapter(Context context, List<CustomInfoDataModel.CustomInfoModel> data) {
        this.context = context;
        this.data = data;
    }

    public void onRefresh(List<CustomInfoDataModel.CustomInfoModel> mList) {
        if (mList != null) {
            if (data == null) {
                data = new ArrayList<>();
            }
            this.data.clear();
            this.data.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<CustomInfoDataModel.CustomInfoModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (data == null) {
                data = new ArrayList<>();
            }
            this.data.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            LayoutEmptyListBinding emptyListBinding = LayoutEmptyListBinding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemCustomInfoBinding binding = ItemCustomInfoBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            CustomInfoDataModel.CustomInfoModel model = data.get(position);

            if (!TextUtils.isEmpty(model.getCreateTime()) && model.getCreateTime().length() > 10) {
                myViewHolder.binding.tvDate.setText(model.getCreateTime().substring(0, 10));
                if (position == 0) {
                    myViewHolder.binding.llDate.setVisibility(View.VISIBLE);
                } else if (position > 0) {
                    if (model.getCreateTime().substring(0, 10).equals(data.get(position - 1).getCreateTime().substring(0, 10))) {
                        myViewHolder.binding.llDate.setVisibility(View.GONE);
                    } else {
                        myViewHolder.binding.llDate.setVisibility(View.VISIBLE);
                    }
                }
            } else {
                myViewHolder.binding.tvDate.setText(CommonUtil.checkText(model.getCreateTime()));
            }

            myViewHolder.binding.topLine.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
            myViewHolder.binding.llLine.setPadding(0, 0, 0, position == data.size() - 1 ? DensityUtil.dip2px(context, 20) : 0);
            myViewHolder.binding.tvType.setText(CommonUtil.setText(model.getImproveTypeValue()));
            myViewHolder.binding.tvContent.setText(CommonUtil.checkText(model.getInfoContent()));
            myViewHolder.binding.editor.setText(Html.fromHtml(context.getString(R.string.editor, CommonUtil.setText3(model.getOperateUserName())), Html.FROM_HTML_MODE_LEGACY));
            myViewHolder.binding.tvInfoSource.setText(Html.fromHtml(context.getString(R.string.info_source_mao, CommonUtil.setText3(model.getInfoSourceValue())), Html.FROM_HTML_MODE_LEGACY));

            List<LocalMedia> imgData = null;
            if (null != model.getFileUrlList()) {
                imgData = new ArrayList<>();
                List<String> urls = model.getFileUrlList();
                for (String s : urls) {
                    LocalMedia localMedia = new LocalMedia();
                    localMedia.setPath(s);
                    imgData.add(localMedia);
                }
            }
            myViewHolder.binding.recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
            myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(imgData));

            myViewHolder.binding.tvContent.setMaxLines(model.isOpen() ? Integer.MAX_VALUE : 3);
            myViewHolder.binding.tvOpen.post(() -> myViewHolder.binding.tvOpen.setVisibility(myViewHolder.binding.tvContent.getLineCount() > 3 ? View.VISIBLE : View.GONE));
            myViewHolder.binding.tvOpen.setText(model.isOpen() ? context.getString(R.string.no_expand) : context.getString(R.string.expand));
            myViewHolder.binding.tvOpen.setOnClickListener(v -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(position);
            });
            myViewHolder.binding.tvContent.setOnClickListener(v -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(position);
            });
        }
    }

    @Override
    public int getItemCount() {
        if (data.size() == 0) {
            return 1;
        }
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (data.size() == 0) {
            return 0;
        }
        return 1;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCustomInfoBinding binding;

        public MyViewHolder(ItemCustomInfoBinding itemCustomInfoBinding) {
            super(itemCustomInfoBinding.getRoot());
            binding = itemCustomInfoBinding;
        }
    }


    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<LocalMedia> mData;

        public MyAdapter(List<LocalMedia> mData) {
            this.mData = mData;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemImage2Binding binding = ItemImage2Binding.inflate(LayoutInflater.from(context), parent, false);
            return new ImageViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ImageViewHolder) {
                ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
                LocalMedia model = mData.get(position);
                Glide.with(context)
                        .load(model.getPath())
                        .centerCrop()
                        .placeholder(R.color.color_F6F6F6)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(imageViewHolder.binding.ivImage);
                imageViewHolder.itemView.setOnClickListener(view -> PictureSelector.create(context)
                        .openPreview()
                        .setImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                        .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                            @Override
                            public void onPreviewDelete(int position) {

                            }

                            @Override
                            public boolean onLongPressDownload(Context context, LocalMedia media) {  // 长按保存图片
                                return false;
                            }
                        })
                        .startActivityPreview(holder.getAbsoluteAdapterPosition(), false, (ArrayList<LocalMedia>) mData));
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {

        ItemImage2Binding binding;

        public ImageViewHolder(ItemImage2Binding itemImageBinding) {
            super(itemImageBinding.getRoot());
            binding = itemImageBinding;
        }
    }

    public static class EmptyViewHolder extends RecyclerView.ViewHolder {

        public EmptyViewHolder(LayoutEmptyListBinding emptyListBinding) {
            super(emptyListBinding.getRoot());
        }
    }
}
